$(function() {

    /*
     * 加载对所有开放的文档数据。
     */
    $.post("/home/search", function(data) {
        if (data.retCode == 0) {
            renderData(data.retVal.list);
        } else {
            console.info("获取数据失败。");
        }
    });

    $("#searchbtn").click(function(data) {
        $.post("/home/search", {
            "name": $("#docSearchInput").val()
        }, function(data) {
            if (data.retCode == 0) {
                renderData(data.retVal.list);
            } else {
                console.info("获取数据失败。");
            }
        });
    });

    $('#docSearchInput').typeahead({
        source: function(query, process) {
            $.get('/home/search?name=' + query, function(data) {
                if (data && data.retCode == 0) {
                    var docList = data.retVal.list;
                    var docs = new Array();
                    if (docList) {
                        docList.forEach(function(obj, index) {
                            docs.push({
                                id: obj.id,
                                name: obj.name
                            })
                        });
                    }
                    process(docs);
                    //renderData(docList);
                }
            });
        },
        items: 10,
        autoSelect: false
    });

    function renderData(list) {
        if (list && list.length) {
            $("#doc_data").empty();
            var str = '';
            for (var i = 0; i < list.length; i++) {
                var obj = list[i];
                var version = obj.latestVersion
                if(null != version && '' != version){
                    str += '<li><a  target="_blank" href="/static/html/doc/release.html?docId=' + obj.id + "&verNo=" +version+'" title="' + obj.name + '">' + obj.name + '</a></li>';
                } else {
                    str += '<li><a  target="_blank" href="./static/html/doc/show.html?' + obj.id + '" title="' + obj.name + '">' + obj.name + '</a></li>';
                }
            }
            $("#doc_data").append("<ul>" + str + "</ul>");
        }
    }


/*    $('#loginBtn').click(function(event) {
        var self = $(this);
        var account = $("#account").val();
        if (loginCheck()) {
            $.post('/loginAjax', { 'account': account, 'password': $("#password").val() }, function(data, textStatus, xhr) {
                if (data && data.retCode == 0) {
                    // 缓存帐号
                    sessionStorage.setItem("account", account);
                    // 
                    $('#no-login').hide();
                    $('#loginUser').prepend(sessionStorage.getItem("account"));
                    $('#isLogin').show();
                    $('#login-modal').modal('hide');
                } else {

                }
            });
        }

    });*/

});
